<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="产品主键">
          <el-input
            v-model="searchParam.productId"
            placeholder="请输入产品主键"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="工序主键">
          <el-input
            v-model="searchParam.processId"
            placeholder="请输入工序主键"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="特性主键">
          <el-input
            v-model="searchParam.paramId"
            placeholder="请输入特性主键"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
<!--        <el-form-item label="创建时间">-->
<!--          <el-date-picker-->
<!--            v-model="searchParam.createTimeSearch"-->
<!--            type="datetimerange"-->
<!--            range-separator="至"-->
<!--            start-placeholder="开始日期"-->
<!--            end-placeholder="结束日期"-->
<!--            value-format="YYYY-MM-DD HH:mm:ss" @change="getcreateTime">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search/>
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh/>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus/>
          新增
        </el-button>
        <el-button type="danger" plain :disabled="ids.length === 0" @click="handleTableDelete()">
          <i-ep-delete/>
          删除
        </el-button>

      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="产品编号" prop="productCode" min-width="100" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="产品名称" prop="productName" min-width="150" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="工序" prop="processCode" min-width="100" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="特性编码" prop="paramCode" min-width="100" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="特性名称" prop="paramName" min-width="150" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="偏差百分比" prop="diffRate" min-width="100" show-overflow-tooltip align="right" header-align="center">
        </el-table-column>
        <el-table-column label="跳动阈值百分比" prop="jumpRate" min-width="140" show-overflow-tooltip align="right" header-align="center">
        </el-table-column>
        <el-table-column label="跳动预警对比数据个数" prop="jumpNumber" min-width="160" show-overflow-tooltip align="right" header-align="center">
        </el-table-column>
        <el-table-column
          label="是否启用"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enabled == 1"
            >启用</el-tag
            >
            <el-tag type="info" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="createdUserName" min-width="100" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="150" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="更新人" prop="lastUpdatedUserName" min-width="100" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column label="更新时间" prop="lastUpdateTime" min-width="150" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-search/>
            </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)">
              <i-ep-edit/>

            </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)">
              <i-ep-delete/>

            </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qdfeaturealarmconfig-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import {useTable, useDownload, useDictionary} from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "ETLQDFEATUREALARMCONFIG",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/pqd-etl/qdfeaturealarmconfig/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  productId: "",
  productCode: "",
  productName: "",
  processId: "",
  processCode: "",
  processName: "",
  paramId: "",
  paramCode: "",
  paramName: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
};

const deleteParam = {
  api: "/pqd-etl/qdfeaturealarmconfig/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {
}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/pqd-etl/qdfeaturealarmconfig/get/${id}`,
    method: "get",
  }).then(data => {
    addDialog.data = data.data;
    addDialog.visible = true;
  }).catch(res => {
    console.log(res)
  });
}

/** 打开特性项检测报警配置表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改特性项检测报警配置表";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增特性项检测报警配置表";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开特性项检测报警配置表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "特性项检测报警配置表详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0]
    searchParam.value.createTimeEnd = data[1]
  } else {
    searchParam.value.createTimeStart = ""
    searchParam.value.createTimeEnd = ""
  }
}


async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const {data} = await request({
      url: `/pqd-etl/qdfeaturealarmconfig/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
</script>
